"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var fs = __importStar(require("fs"));
var path = __importStar(require("path"));
var parser_1 = require("@typescript-eslint/parser");
var utils_1 = require("@typescript-eslint/utils");
var createRule = utils_1.ESLintUtils.RuleCreator(function (name) { return "https://example.com/rule/".concat(name); });
function findClassDeclarationByName(className, ast) {
    var classDeclaration = ast.body.find(function (node) {
        if (node.type === utils_1.AST_NODE_TYPES.ExportNamedDeclaration) {
            node = node.declaration;
        }
        if (node.type === utils_1.AST_NODE_TYPES.ClassDeclaration) {
            return node.id && node.id.type === utils_1.AST_NODE_TYPES.Identifier && node.id.name === className;
        }
        return false;
    });
    if (classDeclaration && classDeclaration.type === utils_1.AST_NODE_TYPES.ExportNamedDeclaration) {
        classDeclaration = classDeclaration.declaration;
    }
    return classDeclaration;
}
function findSuperClassByName(filename, node, className, ast, depth, depthList) {
    if (depth === void 0) { depth = 0; }
    if (depthList === void 0) { depthList = []; }
    if (!node.superClass) {
        return true;
    }
    if (node.superClass.type === "Identifier") {
        console.log("".concat(depth, " ").concat(depthList.length, " ").concat(node.superClass.name));
        if (node.superClass.name === className) {
            if (depthList.length == depth) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            var superClassName = node.superClass.name;
            var superClassNode = findClassDeclarationByName(superClassName, ast);
            if (!superClassNode) {
                var superfilepath = "";
                var bodyCount = ast.body.length;
                for (var i = 0; i < bodyCount; i++) {
                    var node_1 = ast.body[i];
                    if (node_1.type == utils_1.AST_NODE_TYPES.ImportDeclaration) {
                        var importCount = node_1.specifiers.length;
                        console.log(importCount);
                        console.log(node_1.source.value);
                        console.log(superClassName);
                        for (var j = 0; j < importCount; j++) {
                            var spec = node_1.specifiers[j];
                            if (spec.local.type === utils_1.AST_NODE_TYPES.Identifier && spec.local.name === superClassName) {
                                superfilepath = node_1.source.value;
                                break;
                            }
                        }
                    }
                    if (superfilepath != "") {
                        break;
                    }
                }
                var filepath = path.dirname(filename);
                var superFilename = path.join(filepath, superfilepath + ".ts");
                console.log(filepath);
                console.log(superFilename);
                var fileContent = fs.readFileSync(superFilename, "utf-8");
                var superAst = (0, parser_1.parse)(fileContent);
                superClassNode = findClassDeclarationByName(superClassName, superAst);
                if (superClassNode) {
                    depthList.push(true);
                    return findSuperClassByName(filename, superClassNode, className, superAst, depth, depthList);
                }
            }
            if (superClassNode) {
                depthList.push(true);
                return findSuperClassByName(filename, superClassNode, className, ast, depth, depthList);
            }
        }
    }
    return false;
}
function isSubclassOfEntity(filename, node, ast, depth) {
    if (depth === void 0) { depth = 0; }
    return findSuperClassByName(filename, node, "Entity", ast, depth);
}
// Type: RuleModule<"uppercase", ...>
var rule = createRule({
    create: function (context) {
        return {
            ClassDeclaration: function (node) {
                if (node.id != null && node.superClass) {
                    var filename = context.getPhysicalFilename();
                    var code = context.getSourceCode();
                    if (!isSubclassOfEntity(filename, node, code.ast)) {
                        context.report({
                            messageId: "multiple",
                            node: node.id,
                        });
                    }
                }
            }
        };
    },
    name: "entity-class-declaration",
    meta: {
        docs: {
            description: "实体类只能继承自Entity, 禁止多层继承.",
            recommended: "warn",
        },
        messages: {
            multiple: "禁止多层继承.",
        },
        type: "suggestion",
        schema: [],
    },
    defaultOptions: [],
});
module.exports = rule;
